/**
 * 
 */
package org.compiere.process;

import java.math.BigDecimal;
import java.sql.Timestamp;

import org.adempiere.exceptions.AdempiereException;
import org.compiere.model.Mhrmemployee;
import org.compiere.model.Mhrmintercheckoff;
import org.compiere.model.Mhrmintercheckoffline;
import org.compiere.model.Mhrmorder;
import org.compiere.model.Mhrmsalaryemployee;
import org.compiere.util.Env;
import org.compiere.util.Msg;

/**
 * @author A.Yerzhanov
 * Apr 10, 2013
 */

public class GenerateIntercheckoff extends SvrProcess {
	
	private Mhrmintercheckoff inter = null;
	private Mhrmintercheckoffline intline = null;
	/**	Employee Lines	*/
	private Mhrmemployee[] linesEmployee;
	
	private Timestamp dateFrom;
	double SUM = .0;
	BigDecimal amount = null;
	
	@Override
	protected void prepare() {
		
		inter = new Mhrmintercheckoff(getCtx(), getRecord_ID(), null);

	}

	@Override
	protected String doIt() throws Exception {
		
		if(inter.getReversal_ID() != 0)
			return null;
		else if(inter.get_ID() == 0)
			return null;
		
		//String atype = X_hrm_order.HRM_ORDERTYPE_StoppageOfWages;
		dateFrom = inter.getDateFrom();
		Mhrmorder order = new Mhrmorder(getCtx(), inter.gethrm_order_ID(), get_TrxName());
		
		if (inter.getC_BPartner_ID() == 0 && order != null){
			
			linesEmployee = order.getLinesEmployee();		
			//
			if(linesEmployee.length == 0)
				throw new AdempiereException(Msg.translate(Env.getCtx(), "NoEmployee"));
			
			double totalLine = 0.;
			for(Mhrmemployee empl: linesEmployee){
				intline = Mhrmintercheckoffline.getInterline(empl.getC_BPartner_ID(), inter.gethrm_intercheckoff_ID());
				if (intline == null || intline.get_ID() == 0)
					intline = new Mhrmintercheckoffline(getCtx(), 0, null);
				intline.sethrm_intercheckoff_ID(inter.gethrm_intercheckoff_ID());
				intline.setC_BPartner_ID(empl.getC_BPartner_ID());	
				
				if (empl.gethrm_percentBonus().compareTo(new BigDecimal(0)) == 1){
				BigDecimal amount = Mhrmsalaryemployee.getAmount(empl.getC_BPartner_ID(), dateFrom);
					if (amount != null){
						amount = amount.multiply(empl.gethrm_percentBonus().multiply(new BigDecimal(0.01)));
						intline.sethrm_amount(amount);
					}else{
						throw new AdempiereException ("Info about amount was empty for person: " + empl.getC_BPartner().getName() + ". Need to set salary or run timesheet procedure.");
					}
				}else{
					intline.sethrm_amount(empl.gethrm_amountbonus());
					totalLine = totalLine+ empl.gethrm_amountbonus().doubleValue();
				}	
				intline.saveEx();
			}

			if(totalLine != 0){
				inter.sethrm_total(new BigDecimal(totalLine));
				inter.saveEx();
			}
		}else{
			intline = Mhrmintercheckoffline.getInterline(inter.getC_BPartner_ID(), inter.gethrm_intercheckoff_ID());
			if (intline == null || intline.get_ID() == 0)
				intline = new Mhrmintercheckoffline(getCtx(), 0, null);
			intline.sethrm_intercheckoff_ID(inter.gethrm_intercheckoff_ID());
			intline.setC_BPartner_ID(inter.getC_BPartner_ID());
			intline.sethrm_amount(inter.gethrm_total());
		}
		
		intline.saveEx();
	
		return Msg.translate(Env.getCtx(), "Success");
	}

}
